using Unity.Burst;
using Unity.Collections;
using Unity.Entities;
using Unity.Mathematics;
using Unity.Transforms;

partial struct RangeDamageSystem : ISystem
{
    
    [BurstCompile]
    public void OnUpdate(ref SystemState state)
    {
        Entity enemyCounterEntity = SystemAPI.GetSingletonEntity<EnemyCounter>();
        EntityCommandBuffer entityCommandBuffer =
            SystemAPI.GetSingleton<EndSimulationEntityCommandBufferSystem.Singleton>().CreateCommandBuffer(state.WorldUnmanaged);
        //EntityStorageInfoLookup.
        //RangeDamageJob rangeDamageJob = new RangeDamageJob
        //{
        //    deltaTime = SystemAPI.Time.DeltaTime,
        //};
        //rangeDamageJob.ScheduleParallel();
        foreach ((
            RefRO<LocalTransform> localTransform,
            RefRW<RangeDamage> rangeDamage,
            Entity entity)
            in SystemAPI.Query<
                RefRO<LocalTransform>,
                RefRW<RangeDamage>>().WithEntityAccess())
        {
            
            if (rangeDamage.ValueRO.lifeTime <= 0)
            {
                entityCommandBuffer.DestroyEntity(entity);
                continue;
            }
            rangeDamage.ValueRW.lifeTime -= SystemAPI.Time.DeltaTime;
            if (rangeDamage.ValueRO.timer > 0)
            {
                rangeDamage.ValueRW.timer -= SystemAPI.Time.DeltaTime;
                continue;
            }
            rangeDamage.ValueRW.timer = rangeDamage.ValueRO.timerMax;
            EntityQuery entityQuery =
                new EntityQueryBuilder(Allocator.Temp).WithAll<LocalTransform, Enemy>().Build(state.EntityManager);
            NativeArray<Entity> entityArray = entityQuery.ToEntityArray(Allocator.Temp);
            NativeArray<LocalTransform> localTransformArray = entityQuery.ToComponentDataArray<LocalTransform>(Allocator.Temp);
            for (int i = 0; i < entityArray.Length; i++)
            {
                if (math.distancesq(localTransformArray[i].Position, localTransform.ValueRO.Position) < rangeDamage.ValueRO.range * rangeDamage.ValueRO.range)
                {
                    EnemyCounter enemyCounter = state.EntityManager.GetComponentData<EnemyCounter>(enemyCounterEntity);
                    enemyCounter.enemyCount--;
                    state.EntityManager.SetComponentData(enemyCounterEntity, enemyCounter);
                    //UnityEngine.Debug.Log(enemyCounter.enemyCount);
                    entityCommandBuffer.DestroyEntity(entityArray[i]);
                }
            }
            entityArray.Dispose(); localTransformArray.Dispose();
        }
    }

    //[BurstCompile]
    //public partial struct RangeDamageJob : IJobEntity
    //{
    //    //[ReadOnly] public EntityCommandBuffer entityCommandBuffer;
    //    public float deltaTime;
    //    public void Execute(in LocalTransform localTransform, ref RangeDamage rangeDamage, Entity entity)
    //    {
    //        EntityCommandBuffer entityCommandBuffer = new EntityCommandBuffer(Allocator.Temp);
    //        if (rangeDamage.lifeTime <= 0)
    //        {
    //            entityCommandBuffer.DestroyEntity(entity);
    //            return;
    //        }
    //        rangeDamage.lifeTime -= deltaTime;
    //        if (rangeDamage.timer > 0)
    //        {
    //            rangeDamage.timer -= deltaTime;
    //            return;
    //        }
            
    //        EntityManager entityManager = World.DefaultGameObjectInjectionWorld.EntityManager;
    //        rangeDamage.timer = rangeDamage.timerMax;
    //        EntityQuery entityQuery =
    //            new EntityQueryBuilder(Allocator.Temp).WithAll<LocalTransform, Enemy>().Build(entityManager);
    //        NativeArray<Entity> entityArray = entityQuery.ToEntityArray(Allocator.Temp);
    //        NativeArray<LocalTransform> localTransformArray = entityQuery.ToComponentDataArray<LocalTransform>(Allocator.Temp);
    //        for (int i = 0; i < entityArray.Length; i++)
    //        {
    //            if (math.distancesq(localTransformArray[i].Position, localTransform.Position) < 25f)
    //            {
    //                entityCommandBuffer.DestroyEntity(entityArray[i]);
    //            }
    //        }
    //        entityArray.Dispose(); localTransformArray.Dispose();
    //        entityCommandBuffer.Playback(entityManager);
    //    }
    //}
}


